"""
Case Type   : to_binary_float
Case Name   : 通过jdbc驱动执行to_binary_float函数
Create At   : 2024/10/24
Owner       : @li-xin12345
Description :
    1、准备Java脚本
    2、编译执行
    3、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
History     :
"""
import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class ToBinaryFloatTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        text = f'-----{os.path.basename(__file__)} start-----'
        self.log.info(text)
        self.constant = Constant()
        self.common = Common()
        self.pri_sh = CommonSH('PrimaryDbAmode')
        self.pri_root_node = Node('PrimaryRoot')
        self.pri_user_node = Node('PrimaryDbAmode')
        self.script_name = 'To_Binary_Float_Case0027'
        self.dest_path = os.path.join(
            macro.DB_MY_PATH, 'To_Binary_Float_Case0027').replace('\\', '/')

    def test_to_binary_float(self):
        text = '----step1: 准备java脚本;expect: 成功----'
        self.log.info(text)
        self.log.info('--远程复制java脚本至指定主机--')
        self.common.scp_file(self.pri_root_node,
                             f"{self.script_name}.java", self.dest_path)
        cmd = f'ls {self.dest_path}'
        res = self.pri_root_node.sh(cmd).result()
        self.assertIn(self.script_name, res, '远程复制java脚本至指定主机失败')

        self.log.info('--编译java工具--')
        self.pri_root_node.scp_put(macro.JDBC_PATH,
                                   f"{self.dest_path}/postgresql.jar")
        cmd = f"javac -encoding utf-8 -cp " \
              f"{os.path.join(self.dest_path, 'postgresql.jar')} " \
              f"{os.path.join(self.dest_path, f'{self.script_name}.java')}"
        self.log.info(cmd)
        result = self.pri_root_node.sh(cmd).result()
        self.log.info(result)
        self.assertEqual('', result, '编译java工具失败')

        text = '----step2: 运行java脚本;expect: 成功----'
        self.log.info(text)
        url_host_port = f"{self.pri_user_node.db_host}:" \
                        f"{self.pri_user_node.db_port}"
        cmd = f" java -cp {os.path.join(self.dest_path, 'postgresql.jar')}" \
              f":{self.dest_path} " \
              f"{self.script_name} {url_host_port} " \
              f"{self.pri_user_node.db_name} {self.pri_user_node.db_user} " \
              f"{self.pri_user_node.db_password} 2>&1"
        self.log.info(cmd)
        res = self.pri_root_node.sh(cmd).result()
        self.log.info(res)
        self.assertIn('999', res, f'执行失败: {text}')
        self.assertIn('3.402e+38', res, f'执行失败: {text}')
        self.assertIn('-3.402e+38', res, f'执行失败: {text}')
        self.assertIn('Infinity', res, f'执行失败: {text}')
        self.assertIn('-Infinity', res, f'执行失败: {text}')

    def tearDown(self):
        text = '----step3: 清理环境;expect:成功----'
        self.log.info(text)
        cmd = f'drop table if exists t_tbf_0027 cascade;'
        drop_res = self.pri_sh.execut_db_sql(cmd)
        self.log.info(drop_res)
        rm_cmd = f'rm -rf {self.dest_path}'
        self.pri_root_node.sh(rm_cmd).result()
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, drop_res,
                      f'执行失败: {text}')
        text = f'-----{os.path.basename(__file__)} end-----'
        self.log.info(text)